package cn.demo.clickhouse;

import ru.yandex.clickhouse.ClickHouseConnection;
import ru.yandex.clickhouse.ClickHouseDriver;
import ru.yandex.clickhouse.ClickHouseStatement;
import ru.yandex.clickhouse.settings.ClickHouseProperties;

import java.sql.ResultSet;
import java.sql.SQLException;

// https://bbs.huaweicloud.com/blogs/373805
public class Demo01 {
    private static final ClickHouseDriver driver;
    private static ClickHouseConnection connection = null;
    private static ClickHouseStatement statement = null;

    static {
        driver = new ClickHouseDriver();
        try {
            ClickHouseProperties properties = new ClickHouseProperties();
            properties.setUser("sj001");
            properties.setPassword("11111111");
            connection = driver.connect("jdbc:clickhouse://192.168.20.141:18123/sj_db", properties);
            // Java 读取ClickHouse集群表数据
            // BalancedClickhouseDataSource dataSource = new BalancedClickhouseDataSource("jdbc:clickhouse://node1:8123,node2:8123,node3:8123/default", props);
            statement = connection.createStatement();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        // insert();
        query();
    }

    private static void close() {
        try {
            statement.close();
            connection.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    private static void close(ResultSet resultSet) {
        try {
            if (resultSet!= null) {
                resultSet.close();
            }
            statement.close();
            connection.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    private static void insert() {
        String sql = "INSERT INTO arrays_test\n" +
                "VALUES ('Hello', [1,2]), ('World', [3,4,5]), ('Goodbye', []);";
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO arrays_test VALUES ");
        sb.append("('吊毛', [11,12,13]),");
        sb.append("('隔壁老王', [22,34,45]),");
        sb.append("('阿三哥', [111,112,113]),");
        try {
            boolean execute = statement.execute(sb.toString());
            if (execute){
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败: sql="+sb);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            close();
        }
    }

    public static void query() {
        ResultSet resultSet = null;
        try {
            resultSet = statement.executeQuery("select s,arr from sj_db.arrays_test array join arr");
            while (resultSet.next()) {
                System.out.println(resultSet.getString("s") + " " + resultSet.getString("arr"));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }finally {
            close(resultSet);
        }
    }
}
